﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using GMap.NET.WindowsForms;
using GMap.NET;

namespace QiaoGIS.Core.Controls
{
    public partial class QUCMapTOC : QGUserControl
    {
        #region 类成员变量

        private IQGApplication _qgApp;

        #endregion

        #region 类构建与初始化

        /// <summary>
        /// 初始化控件
        /// </summary>
        public override void InitializeControls()
        {
            if (this.treeListLayers == null) return;
            this.treeListLayers.Nodes.Clear();
            this.treeListLayers.Dock = DockStyle.Fill;
            this.tlColumnLayer.OptionsColumn.AllowEdit = false;
            this.treeListLayers.MouseClick += new MouseEventHandler(treeListLayers_MouseClick);
        }

        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void treeListLayers_MouseClick(object sender, MouseEventArgs e)
        {
            int i = -1;
            //for (i = 2; i < map.Overlays.Count; i++)
            //    map.Overlays[i].IsVisibile = this.treeListLayers.Nodes[i - 2].Checked;

            QUCMapControl map = this._qgApp.MapControl;

            for (i = 0; i < this.treeListLayers.Nodes.Count; i++)
            {
                map.Overlays[i + 2].IsVisibile = treeListLayers.Nodes[i].Checked;
            }
            // 刷新地图
            map.Refresh();
        }


        #endregion

        #region 类属性

        /// <summary>
        /// 应用程序对象
        /// </summary>
        public override IQGApplication QGApplication
        {
            get
            {
                return _qgApp;
            }
            set
            {
                if (treeListLayers == null) return;
                this.treeListLayers.Nodes.Clear();

                if (value == null) return;
                _qgApp = value;
                QUCMapControl map = value.MapControl;
                if (map == null) return;

                for (int i = 2; i < map.Overlays.Count; i++)
                {
                    this.treeListLayers.AppendNode(new object[] { map.Overlays[i].Id }, -1, (CheckState)Convert.ToInt32(map.Overlays[i].IsVisibile));
                }
            }
        }
       
        #endregion
    }
}
